import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login'
import Home from '../components/Home'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login, name: 'Login' },
  { path: '/home', component: Home, name: 'Home' }
]

const router = new VueRouter({
  routes
})
router.beforeEach((to, form, next) => {
  // console.log(to, form)
  // 先判断是否是登录页面
  if (to.path === '/login') return next()
  // 其他页面判断是否有token
  let tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

export default router
